//
//  NewscontentViewController.m
//  News
//
//  Created by weige on 16/5/13.
//  Copyright © 2016年 weige. All rights reserved.
//

#import "NewscontentViewController.h"
#import "AppDelegate.h"
@interface NewscontentViewController ()
{
    FMDatabase *db;
    UIBarButtonItem *bar;
}
@end

@implementation NewscontentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor whiteColor];
    bar=[[UIBarButtonItem alloc]
         initWithTitle:@"添加收藏" style:UIBarButtonItemStyleDone target:self action:@selector(shoucang:)];
    //[bar setTitle:@"收藏"];
    [[UIBarButtonItem appearance] setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14.0F],NSForegroundColorAttributeName:[UIColor whiteColor]} forState:UIControlStateHighlighted];
    self.navigationItem.rightBarButtonItem=bar;
    [self initNewDB];
    [self initNewsView];
}

-(void)initNewsView
{
    self.automaticallyAdjustsScrollViewInsets=NO;

    UIScrollView *sv=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 60, self.view.frame.size.width, self.view.frame.size.height-60-60)];
    sv.scrollEnabled=YES;
    [self.view addSubview:sv];
    //sv.backgroundColor=[UIColor redColor];
    UILabel *titlelable=[[UILabel alloc]init];
 
    
    //titlelable.frame=CGRectMake(120, 200, 100, 30);
    titlelable.font=[UIFont  systemFontOfSize:20.0F weight:2 ];
    
    titlelable.textColor=[UIColor blackColor];
    titlelable.textAlignment=NSTextAlignmentCenter;
    titlelable.numberOfLines=0;
   [sv addSubview:titlelable];
    
    CGSize size=[self.news.title boundingRectWithSize:CGSizeMake(self.view.frame.size.width-60, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20.0F],NSForegroundColorAttributeName:[UIColor blackColor]} context:nil].size;
    NSLog(@"%f",size.height);
    CGFloat titleX=ScreenBounds.size.height/2-size.width;
    CGFloat titleY=10;
    titlelable.frame=CGRectMake(titleX, titleY, size.width, size.height);
    
    titlelable.text=self.news.title;
    

    UILabel *authorlable=[[UILabel alloc]init];
    authorlable.textColor=SUBTITLECOLOR;
    authorlable.font=[UIFont systemFontOfSize:SubtitleFontsize];
    CGSize authorsize=[[NSString stringWithFormat:@"来自%@",self.news.author ] sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:SubtitleFontsize]}];
    authorlable.frame=CGRectMake((ScreenBounds.size.width-authorsize.width)/2, titleY+authorsize.height+size.height+10,authorsize.width , authorsize.height);
    authorlable.text=[NSString stringWithFormat:@"来自%@",self.news.author ];
    [sv addSubview:authorlable];
    UILabel *timelable=[[UILabel alloc]init];
    timelable.textColor=SUBTITLECOLOR;
    timelable.font=[UIFont systemFontOfSize:SubtitleFontsize];
    CGSize timesize=[[NSString stringWithFormat:@"%@",self.news.createDate]sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:SubtitleFontsize]}];
    
    authorlable.frame=CGRectMake((ScreenBounds.size.width-authorsize.width)/2-timesize.width, titleY+authorsize.height+size.height+10,authorsize.width , authorsize.height);
    timelable.frame=CGRectMake(authorsize.width+timesize.width+20, titleY+authorsize.height+size.height+10, timesize.width, timesize.height);
    timelable.text=[NSString stringWithFormat:@"%@",self.news.createDate];
    [sv addSubview: timelable];
    UILabel *content=[[UILabel alloc]init];
    content.textColor=[UIColor blackColor];
    content.numberOfLines=0;
    content.font=[UIFont systemFontOfSize:14.0f];
    CGSize contentsize=[self.news.content boundingRectWithSize:CGSizeMake(SCREEN_WIDTH-20, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin   attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14.0F]} context:nil].size;
    content.frame=CGRectMake(10, titleY+authorsize.height+size.height+10+20, contentsize.width, contentsize.height);
    content.text=self.news.content;
    [sv addSubview:content];
    sv.contentSize=CGSizeMake(SCREEN_WIDTH ,titleY+authorsize.height+size.height+contentsize.height+60);

   
   
    
    NSLog(@"w=%f,sv-h=%f,x=%f,y=%f",sv.frame.size.width,sv.contentSize.height,sv.frame.origin.x,sv.frame.origin.y);
   // sv.contentSize=CGSizeMake(320, 9000);
  
    

    
}

-(void)shoucang:(UIBarButtonItem *)sender
{
    
    NSString * title=sender.title;
    if([title isEqualToString:@"添加收藏"])
    {
        if([db executeUpdate:@"insert into t_news values(?,?,?,?,?,?,?,?,?)",[NSNumber numberWithInt:self.news.idid],self.news.title,self.news.subtitle,self.news.createDate,self.news.content,[NSNumber numberWithInt:self.news.flid],self.news.author,[NSNumber numberWithInt:self.news.click],self.news.data ]==YES)
        {
            //[ZHQUtil alert:@"恭喜，收藏成功！" andVC:self];
        }
        else
        {
            //[ZHQUtil alert:@"对不起，收藏失败！" andVC:self];
        }
        sender.title=@"取消收藏";
    }
    else
    {
        if([db executeUpdate:@"delete from t_news where idid=?",[NSNumber numberWithInt:self.news.idid]]==YES)
        {
            //[ZHQUtil alert:@"恭喜，取消成功！" andVC:self];
        }
        else
        {
           // [ZHQUtil alert:@"对不起，取消失败！" andVC:self];
        }
        sender.title=@"添加收藏";
    }

}

-(void)initNewDB
{
    AppDelegate *cuuapp=[UIApplication sharedApplication].delegate;
    db=cuuapp.db;
        FMResultSet * rs=[db  executeQuery:@"select * from t_news where idid=?",[NSNumber numberWithInt:self.news.idid]];
    if([rs next])
    {
        bar.title=@"取消收藏";
    }
    [rs close];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
